local print = release_print

function wxCreateExport(fileName) 
    local ui = require(fileName).create()
    if ui.root then
        local new_widget = ccui.Widget:create()
        new_widget:setAnchorPoint(cc.p(0, 0))

        local children = ui.root:getChildren()
        for _, child in pairs(children) do
            child:removeFromParent()
        end
        for _, child in pairs(children) do
            new_widget:addChild(child)
        end

        local name = ui.root:getName()
        -- 自适应代码
        if name ~= "Layer" and name ~= "Node" then
            local visible = cc.Director:getInstance():getVisibleSize()
            new_widget:setContentSize(visible)
            ccui.Helper:doLayout(new_widget)
        else
            new_widget:setContentSize(ui.root:getContentSize())
        end

        rawset(ui, "root", new_widget)
        return new_widget, ui
    end
    return nil
end

function GetNodeTypeName(node)
    if node and not tolua.isnull(node) then
        return tolua.type(node)
    end
    return "error_type"
end
local txt = nil
function GetNodeName(node)
    local name = node:getName()
    if not name or name == "" or name == nil then
        name = GetNodeTypeName(node)
        if not name then
            name = "error_name"
        end
    end    
    if node.__cname then 
        name = name .. "【".. node.__cname.. "】"
    end
    if node.getString then
        txt = node:getString()   
        if txt and txt~="" then       
            name = name .. "“".. txt.. "”"
        end
    end
    if node.getTitleText then  
        txt = node:getTitleText() 
        if txt and txt~="" then       
            name = name .. "“".. txt .. "”"
        end
    end
    return name
end

function GetNodeHashName(node)
    if node and not tolua.isnull(node) then
        return tostring(node)
    end
    return nil
end

function GetNodePanentCount(node)
    if not node.parentCount then
        if node:getParent() then
            node.parentCount = GetNodePanentCount(node:getParent()) + 1
        else
            node.parentCount = 0
        end
    end
    return node.parentCount
end

function CheckNodeVaild(node)
    if not node then
        return false
    end
    if tolua.isnull(node) then
        return false
    end
    if not node.convertToWorldSpace then
        return false
    end
    if not node.getPosition then
        return false
    end
    if not node.getAnchorPoint then
        return false
    end
    if not node.getScaleX then
        return false
    end
    if not node.getContentSize then
        return false
    end
    if not node.getParent or not node:getParent() then
        return false
    end
    return true
end

function wxFindAllChildrenWithType(parent, type)
    local deepFind  = nil 
    local find = {}
    deepFind = function(children , type)
        for i = 1, #children do
            local child = children[i]
            if child and tolua.iskindof(child, type) then
                table.insert(find, child)
            end            
            deepFind(child:getChildren(), type)            
        end    
    end
    deepFind(parent:getChildren(),type)
    return find
end

function wxGetFileNameByPath(path) 
    return path:match("([^/]+)$")
end

function wxFindPathByFileName(fileName) 
    -- 在Windows环境下使用dir命令配合findstr进行查找
    local command = 'dir /S /B "' .. fileName .. '"'
    local handle = io.popen(command)
    
    -- 遍历输出结果
    for file in handle:lines() do
        handle:close()
        return file  -- 返回找到的第一个匹配的完整路径
    end
    handle:close()    
    return nil  -- 如果没有找到，则返回nil
end

-- 在运行时获取调用栈信息以查找某个函数的来源位置
function wxFindFunctionSource(object)
    if object then 
        local info = debug.getinfo(object, "S")
        local name_to_find = wxGetFileNameByPath(info.source)
        local fullPath = wxFindPathByFileName(name_to_find)
        if fullPath then
            return fullPath         
        end
    end
    return nil
end

function wxGetNodeUsefullFunction(node) 
    if not node then 
        return nil
    end
    return node.ctor or node.init or node.create
end

function wxCheckDrawEnable(node) 
    if not CheckNodeVaild(node) then 
        return false
    end

    local contentSize = node : getContentSize()
    if contentSize.width > 0 and contentSize.height > 0 then
        if tolua.iskindof(node, "ccui.Layout") then 
            return false 
        end
        if tolua.iskindof(node, "cc.Sprite") then 
            return true 
        end
        if tolua.iskindof(node, "cc.Label") then 
            return true 
        end
        if tolua.iskindof(node, "ccui.Button") then 
            return true 
        end
        if tolua.iskindof(node, "ccui.ImageView") then 
            return true 
        end
        if tolua.iskindof(node, "ccui.Text") then 
            return true 
        end
    end 
    return false     
end

function wxOr(num1, num2)
    local tmp1 = num1
    local tmp2 = num2
    local str = ""
    repeat
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 == s2 then
            if s1 == 0 then
                str = "0" .. str
            else
                str = "1" .. str
            end
        else
            str = "1" .. str
        end
        tmp1 = math.modf(tmp1 / 2)
        tmp2 = math.modf(tmp2 / 2)
    until (tmp1 == 0 and tmp2 == 0)
    return tonumber(str, 2)
end

function wxAnd(num1, num2)
    local tmp1 = num1
    local tmp2 = num2
    local str = ""
    repeat
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 == s2 then
            if s1 == 1 then
                str = "1" .. str
            else
                str = "0" .. str
            end
        else
            str = "0" .. str
        end
        tmp1 = math.modf(tmp1 / 2)
        tmp2 = math.modf(tmp2 / 2)
    until (tmp1 == 0 and tmp2 == 0)
    return tonumber(str, 2)
end

function wxReloadLayer(file) 
    package.loaded[file] = nil
    require(file)
end

function wxTonumber(num)
    local _ts = string.split(num, ".")
    if _ts and #_ts ~= 1 then
        if _ts and #_ts ~= 2 then
            return nil
        end
        if #_ts == 2 and _ts[2] == "" then
            return nil
        end        
    end
    local _num = tonumber(num)
    return _num ~=0 and _num or nil 
end

function wxStringToUTF8(str)
     return wx.wxString(str):ToUTF8()
end

function UTF8TowxString(str)
    str = str or ""
    return wx.wxString.FromUTF8(str)
end
local nodePos
local anchorPoint
local anx , any

function HitTest(node, pos)
    if not pos then 
        return false
    end
    local contentSize = node : getContentSize()
    if contentSize.width > 0 and contentSize.height > 0 then
        nodePos = node : convertToNodeSpace(pos)
        anchorPoint = node:getAnchorPoint()
        if nodePos.x>=0 and nodePos.x<=contentSize.width and nodePos.y>=0 and nodePos.y<=contentSize.height then
            return true
        end
    end
    return false
end

function wxDelayCall(node, callback, delay)
    local delay = cc.DelayTime:create(delay)
    local sequence = cc.Sequence:create(delay, cc.CallFunc:create(callback))
    node:runAction(sequence)
    return sequence
end

function wxCalcCameraZoom( camera )
    local winSize = cc.Director:getInstance():getWinSize()
    local rtPos = cc.p( winSize.width, winSize.height ) -- right-top
    local lbPos = cc.p( 0, 0 )                          -- left-bottom
    local rtPosGL = camera:projectGL( rtPos )
    local lbPosGL = camera:projectGL( lbPos )
    local rbPosGL = camera:projectGL( cc.p( winSize.width, 0 ) )
    local ltPosGL = camera:projectGL( cc.p( 0, winSize.height ) )

    local widthX = rbPosGL.x - lbPosGL.x
    local widthY = rbPosGL.y - lbPosGL.y
    local heightX = ltPosGL.x - lbPosGL.x
    local heightY = ltPosGL.y - lbPosGL.y

    local zoomWidth = math.sqrt( widthX* widthX + widthY * widthY )
    local zoomHeight = math.sqrt( heightX* heightX + heightY * heightY)

    -- local zoomWidth = rtPosGL.x - lbPosGL.x
    -- local zoomHeight = rtPosGL.y - lbPosGL.y

    local scaleX = winSize.width / zoomWidth
    local scaleY = winSize.height / zoomHeight

    return scaleX, scaleY
end

function wxTouchPanel2World( srcPos, camera,uiCamera )
    local cameraPos = cc.p(camera:getPosition())
    local scaleX, scaleY = wxCalcCameraZoom( camera )
    local p = cc.p( srcPos.x * scaleX, srcPos.y * scaleY ) 
    if camera ~= uiCamera then          
        local winSize = cc.Director:getInstance(): getWinSize()
        local centerOfView = cc.p( winSize.width * 0.5 * scaleX, winSize.height * 0.5 * scaleY )
        return cc.pSub(cc.pAdd( cameraPos, p ), centerOfView )
    end
    return cc.pAdd( cameraPos, p )
end

function wxWorld2TouchPanel( srcPos,camera,uiCamera)    
    if camera ~= uiCamera then 
        local cameraPos = cc.p(camera:getPosition())
        local uiCameraPos = cc.p(uiCamera:getPosition())
        local scaleX, scaleY = wxCalcCameraZoom( camera )    
        local scalexx, scaleyy = wxCalcCameraZoom( uiCamera )    
        local winSize = cc.Director:getInstance():getWinSize()
        local centerOfView = cc.p( winSize.width * 0.5*scaleX, winSize.height * 0.5 *scaleY )
        local p = cc.pAdd( cc.pSub( srcPos, cameraPos ), centerOfView )
        p.x = p.x / scaleX * scalexx
        p.y = p.y / scaleY * scalexx
        return cc.pAdd(p,uiCameraPos)       
    end
    return srcPos
end 

function wxGetNodeWorldContentSize(rectNode)
    -- 获取节点自身的内容大小
    local contentSize = rectNode:getBoundingBox()
    local scaleX = 1
    local scaleY = 1
    local parent = rectNode:getParent()
    while parent do 
        scaleX = scaleX * parent:getScaleX()
        scaleY = scaleY * parent:getScaleY()       
        parent = parent:getParent()
    end 
    return {width = contentSize.width * scaleX, height = contentSize.height * scaleY}
end

function wxFullpath2Filename(fullpath)
    if  fullpath and fullpath~= "" then 
        local start, stop = string.find(fullpath, "game(\\.*)")
        if start then 
            return string.gsub(string.sub(fullpath, start),"\\","/")
        end
    end
    return nil
end

function wxStringEndswith(str, endstr) 
    return endstr=="" or (string.sub(str,-#endstr) == endstr)
end

cc = cc or {}
cc.p =
    cc.p or
    function(x, y)
        return {
            x = x,
            y = y
        }
    end
cc.c4f =
    cc.c4f or
    function(r, g, b, a)
        return {
            r = r,
            g = g,
            b = b,
            a = a
        }
    end
